﻿using BookManege.entity;
using MySql.Data.MySqlClient;
using System.Drawing.Imaging;

namespace BookManege.service
{
    internal class loginService
    {
        //画验证码
        public Verification Verification()
        {
            using(MemoryStream memoryStream = new MemoryStream())
            {
                string ChkCode = string.Empty;
                //颜色
                Color[] color = { Color.Black, Color.Red, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };
                //字体
                string[] font = { "Times New Roman", "MS Mincho", "Book Antique", "Gungsuh", "PmING lIu", "Impact" };
                //字符
                char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'R' };
                Random rnd = new ();
                //character取出随机四个
                for (int i = 0; i < 4; i++)
                {
                    ChkCode += character[rnd.Next(character.Length)];
                }
                Bitmap bmp = new (150, 60);
                Graphics g = Graphics.FromImage(bmp);
                g.Clear(Color.White);
                //画随机长度线
                for (int i = 0; i < 5; i++)
                {
                    int x1 = rnd.Next(120);
                    int x2 = rnd.Next(30);
                    int y1 = rnd.Next(120);
                    int y2 = rnd.Next(30);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawLine(new Pen(clr), x1, x2, y1, y2);
                }
                //合成
                for (int i = 0; i < ChkCode.Length; i++)
                {
                    string fnt = font[rnd.Next(font.Length)];
                    Font ft = new (fnt, 16);
                    Color clr = color[rnd.Next(color.Length)];
                    g.DrawString((ChkCode[i].ToString()), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);
                }
                MemoryStream ms = new ();
                try
                {
                    bmp.Save(ms, ImageFormat.Png);
                    return new Verification(ms,ChkCode);
                }
                finally
                {
                    bmp.Dispose();
                    g.Dispose();
                }
        }
}
        public bool login(string username,string password)
        {
            MySqlConnection connection = DatabaseUtil.GetMysqlConnection();
            string sql = "select * from user where user_name= '" + username+"'";
            MySqlCommand mySqlCommand = new(sql,connection);
            MySqlDataReader result = mySqlCommand.ExecuteReader();
            if (result.Read())
            {
                if (result.GetString("password") == password)
                {
                    MessageBox.Show("登录成功！");
                    long id = result.GetInt64("id");
                    string avatar = result.GetString("avatar");
                    string userName = result.GetString("user_name");
                    string nickName = result.GetString("nick_name");
                    int sex = result.GetInt32("sex");
                    int age = result.GetInt32("age");
                    int phone = result.GetInt32("phone");
                    string email = result.GetString("email");
                    int authority = result.GetInt32("authority");
                    Program.user= new User(id, avatar, userName, "", nickName, sex, age, phone, email, authority);
                    return true;
                }
                else
                {
                    MessageBox.Show("密码错误！");
                    return false;
                }
            }
            else
            {
                MessageBox.Show("用户不存在");
                return false;
            }
        }
    }
}
